//index.js
//获取应用实例
const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    name: "", //姓名
    userCode: "", //工号
    userHas: false,
    imgUrl: '../images/banner.png',
    openIds: ''
  },
  onShow: function (options) {
    let _this = this;
    wx.login({
      success: res => {
        wx.request({
          url: getApp().data.servsers + '/auth?code=' + res.code,
          method: 'get',
          success(res) {
            if (res.data.status == 'success') {
              let openIds = JSON.parse(res.data.data).openid
              wx.setStorage({
                key: "openId",
                data: openIds
              })
              getApp().data.openId = openIds;
              wx.request({
                url: getApp().data.servsers + '/user/findUserByOpenId',
                method: 'POST',
                header: {
                  'content-type': 'application/json' // 默认值
                },
                data: {
                  openId: openIds
                },
                success(res) {
                  if (res.data.status == 'success') {
                    if (res.data.data) {
                      wx.setStorage({
                        key: "userId",
                        data: res.data.data.id
                      })
                      getApp().data.userId = res.data.data.id;
                    } else {
                      _this.setData({
                        userHas: true
                      })
                    }
                  }
                }
              })
            }
          }
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let _this = this;
    wx.request({
      url: getApp().data.servsers + '/imageUrl/findByType',
      method: 'POST',
      header: {
        'content-type': 'application/json' // 默认值
      },
      data: {
        type: "banner"
      },
      success(res) {
        if (res.data.status == 'success') {
          if (res.data.data.img) {
            _this.setData({
              imgUrl: res.data.data.img
            })
          }
        }
      }
    })
  },
  getUserName(e) {
    this.setData({
      name: e.detail.value
    })
  },
  getUserNo(e) {
    this.setData({
      userCode: e.detail.value
    })
  },
  userAdd() {
    //新增用户
    if (this.data.name == '') {
      wx.showModal({
        title: '提示',
        content: '请输入您的姓名',
        success: function (res) { }
      })
      return false;
    }
    if (this.data.userCode == '') {
      wx.showModal({
        title: '提示',
        content: '请输入您的工号',
        success: function (res) { }
      })
      return false;
    } else {
      if (this.data.userCode.length != 7 && this.data.userCode.length != 8) {
        wx.showModal({
          title: '提示',
          content: '请输入正确的工号',
          success: function (res) { }
        })
        return false;
      }
    }
    let _this = this;
    let datas = {
      name: this.data.name,
      workNo: this.data.userCode,
      openId: getApp().data.openId
    }
    datas = JSON.stringify(datas)
    wx.request({
      url: getApp().data.servsers + '/user/add',
      data: datas,
      header: {
        'content-type': 'application/json' // 默认值
      },
      method: 'POST',
      success(res) {
        if (res.data.status == 'success') {
          _this.setData({
            userHas: false
          })
          wx.showModal({
            title: '提示',
            content: '登录成功！',
            success: function (res) {
              wx.request({
                url: getApp().data.servsers + '/user/findUserByOpenId',
                method: 'POST',
                header: {
                  'content-type': 'application/json' // 默认值
                },
                data: {
                  openId: getApp().data.openId
                },
                success(res) {
                  if (res.data.status == 'success') {
                    wx.setStorage({
                      key: "userId",
                      data: res.data.data.id
                    })
                    getApp().data.userId = res.data.data.id;
                  }
                }
              })
            }
          })
        }
      }
    })
  }
})